<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/taglibs.jsp" %>
<script type="text/javascript">
function selectCf() {
	var url= "<s:property value='contextPath'/>/cm/choose.do?currentPage=${currentPage}&search.state=2";
	
	var v1=$("select[name='entity.financial.id']").val();
	var v2=$("input[name='entity.shop.id']").val();
	if(!v2){
		v2=$("select[name='entity.shop.id']").val();
	}
	//var v3=$("select[name='entity.brand.id']").val();
	var ids=$("#sel_cf_ids").val();
	if(v1!=null&&v1!='')
		url+="&search.financial.id="+v1;
	if(v2!=null&&v2!='')
		url+="&search.shop.id="+v2;
	//if(v3!=null&&v3!='')
		//url+="&search.brand.id="+v3;
	if(ids!=null&&ids!='')
		url+="&ids="+ids;
	var vals=$("#sel_cf_vals").val();
	if(vals!=null&&vals!='')
		url+="&vals="+vals;
	var price=$("#supplyPrice").text();
	if(price){
		url+="&price="+price;
	}
	
	window.open(url,'选择合格证','height=900,width=1024,top=0,left=200,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
}

function selectShop(){
	window.open("<s:property value='contextPath'/>/system/treaty/selectShop.do", "newwindow", "height=800, width=800, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
}
function selectShopCallBack(num_,name_,id_){
	if(id_ != $("#shop_id_").val()){
		clearChooseCfs();
	}

	$("#shop_id_").val(id_);
	$("#shop_name_").val(name_);
	doAjaxBrand(id_, 0);
	doAjaxFinancial(id_);
}	

function del(e,id){
	if(confirm("附件操作不可恢复,确认删除吗?")){
		var f=false;
		$.ajax({
			  async:false,
			  type: 'POST',
			  url: "<s:property value='contextPath'/>/attach/del.do?id="+id,
			  dataType: "json",
			  success: function(data) {
					  	f = data.flag;
					  	if(f){
					  		//alert("删除成功");
					  		$(e).parent().remove();
					  	}
					}		
				,
				error:function(data){
				}
			}); 
		
	}
	
}

function resetData(){
	var obj = document.getElementById("shopIdField").value;
	doAjaxFinancial(obj);
	doAjaxBrand(obj, 0);
}

function doAjaxFinancial(i){
	if(i != null){
		var select=document.getElementById("financialField");
	    select.length=0;
		$.ajax({
			  async:false,
			  type: 'POST',
			  url:"<s:property value='contextPath'/>/cm/shopFinal.do?shopIdLong="+i,
			  dataType: "json",
			  success: function(data) {
				  if(data !=null){
					  var obj=document.getElementById("financialField");
					  obj.options.length=0;
					  var a=new Array();
					  var b=new Array();  
					  a = data.toString().split(",");
					  for(var i=0;i<a.length;i++){
						  b = a[i].split(":");
						  obj.options.add(new Option(b[1],b[0]));
					  }
				  }
				  
			  }
			});
	}
	
}
function doAjaxBrand(i, brandId){
	if(i != null){
		var select=document.getElementById("brandField");
	    select.length=0;
		$.ajax({
			  async:false,
			  type: 'POST',
			  url:"<s:property value='contextPath'/>/cm/detailsBrand.do?shopIdLong="+i,
			  dataType: "json",
			  success: function(data) {
				  if(data !=null){
					  var obj=document.getElementById("brandField");
					  obj.options.length=0;
					  obj.options.add(new Option("***请选择***","0"));
					  var a=new Array();
					  var b=new Array();  
					  a = data.toString().split(",");
					  for(var i=0;i<a.length;i++){
						  b = a[i].split(":");
						  var option = new Option(b[1],b[0]);
						  obj.options.add(option);
						 
						  if(brandId == b[0]){
							  option.selected="selected";
						  }
					  }
				  }
				  
			  }
			});
	}
	
}
//function editData(id) {
//	var searchForm = document.form1;
//	searchForm.action = "<s:property value='contextPath'/>/cm/renew/incf/edit.do?currentPage=${currentPage}&entity.id="+id;
//	searchForm.submit();
//}
	
function deleteData(id) {
	if(confirm("确认要删除吗?")) {
		//var searchForm = document.form1;
		window.location = "<s:property value='contextPath'/>/cm/renew/incf/destory.do?currentPage=${currentPage}&entity.id="+id;
		//searchForm.action = "<s:property value='contextPath'/>/cm/renew/incf/destory.do?currentPage=${currentPage}&entity.id="+id;
		//searchForm.submit();
	}	  
}

function clearChooseCfs(){
	$("#sel_cf_ids").val("");
	$("#sel_cf_vals").val("");
	$("#supplyPrice").text("0.0");
	
}

$().ready(function(){
		var shopId = '${entity.shop.id}';
		doAjaxFinancial(shopId);
		doAjaxBrand(shopId, ${entity.brand.id == null ? 0 : entity.brand.id});
	});
</script>
	<input type="hidden" name="redirectUrl" value='<s:property value="redirectUrl"/>'/>
	<font color="red">
		<s:actionerror/>
	</font>
	<table width="100%" border="0" cellpadding="1" cellspacing="1">
		<tr>
			<td class="left-td-width-bg">
				<font color="red" size="2">* </font>4S店：
			</td>
			<td width="45%" class="bline" align="left">
				<s:if test="shoper">
				  <select dataType="Require" msg="请选择4S店 " onchange="resetData();" class="input" name="entity.shop.id" id="shopIdField">
			        <option value="">****请选择****</option>
			         <s:iterator value="shops" status="i" id="b"> 
			         	 <option <s:if test='entity.shop !=null && entity.shop.id==#b.id'>selected="selected"</s:if> value="${b.id}">${b.shopName}</option>
			          </s:iterator>
			      </select>
				</s:if>
				<s:else>
					<input type="hidden" id="shop_id_" name="entity.shop.id" value="<s:property value='entity.shop.id'/>">
					
					<input name="entity.shop.shopName" class="input_send_readOnly_92"  type="text" id="shop_name_" size="16" readonly="readonly"
					style="width: 200px" value="<s:property value='entity.shop.shopName'/>" onclick="javascript:selectShop();"  dataType="Require" msg="店铺必须选择"/>
					<a href="javascript:selectShop()"><img src="${contextPath}/images/chakan.gif" alt="选择店铺" width="16" height="18" border="0" /></a>
				</s:else>
			</td>		
			<td class="left-td-width-bg">
				<font color="red" size="2">* </font>金融机构：
			</td>
			<td width="30%" class="bline" align="left">
				<select id="financialField" name="entity.financial.id" dataType="Require" msg="金融机构必须选择" class="select"></select>
			</td>
		</tr>
		
		
		<tr>
			<td class="left-td-width-bg">
				渠道：
			</td>
			<td width="30%" class="bline" align="left">
				<select id="brandField" name="entity.brand.id" class="select"></select>
			</td>
			<td class="left-td-width-bg">
				申请单号：
			</td>
			<td width="45%" class="bline" align="left">
				<input name="entity.applyNum" class="input_send_readOnly_92" type="text" id="order" size="16"
					style="width: 200px" value="<s:property value='entity.applyNum'/>" readonly="readonly" />
			</td>
		</tr>
		<tr>
			<td class="left-td-width-bg">
				<font color="red" size="2">* </font>申请人：
			</td>
			<td width="30%" class="bline" align="left">
				<input type="hidden" name="entity.applyUser.id" value="<s:property value='entity.applyUser.id'/>">
				<input name="entity.applyUser.realName" class="input_send_readOnly_92"  type="text" id="name" size="16"
					style="width: 200px" value="<s:property value='entity.applyUser.realName'/>" readonly="readonly" />
			</td>
			<td class="left-td-width-bg">
				附件上传：
			</td>
			<td width="45%" class="bline" align="left">
				<input name="file"  type="file" id="file" size="16" style="width: 200px" />
				<s:iterator value="attachs" >
				<div style="display: inline">
					<a href="${contextPath}/attach/download.do?id=<s:property value="id"/>" ><s:property value="name"/></a>&nbsp;<img name="img_del" id="<s:property value="id"/>" src="${contextPath}/images/del.gif" alt="删除" width="16" height="18" border="0" onclick="javascript:del(this,<s:property value="id"/>)" />
				</div>
				</s:iterator>
			</td>
		</tr>
			<tr>
			<td class="left-td-width-bg">
				换出总金额：
			</td>
			<td width="30%" class="bline" align="left">
				<span id="supplyPrice"><s:property value="formatAmount(entity.supplyPrice)"/></span>
			</td>
			<td class="left-td-width-bg">
				换入总金额：
			</td>
			<td width="45%" class="bline" align="left">
				<span id="inMoney"><s:property value="formatAmount(entity.inSupplyPrice)"/></span>
			</td>
		</tr>	
		<tr style="padding-bottom: 20px">
			<td class="left-td-width-bg">
				<font color="red" size="2">* </font>换出车架号：
			</td>
			<td class="bline" align="left" colspan="3">
				<textarea id="sel_cf_vals" rows="4" class="input_textarea_width90_readOnly" style="width: 660px" readonly="readonly" ><s:property value='names'/></textarea><a href="javascript:selectCf()"  dataType="Require" msg="至少选择一辆车" ><img src="${contextPath}/images/chakan.gif" alt="选择车架号" width="16" height="18" border="0" /></a>
				<input type="hidden" name="ids" id="sel_cf_ids" value="<s:property value='ids'/>">
			</td>
		</tr>
		<tr style="padding-bottom: 20px">
			<td class="left-td-width-bg">
				申请理由：
			</td>
			<td class="bline" align="left" colspan="3" >
				 <textarea name="msg" cols="30" rows="5" id="textfield"><s:property value="entity.msg"/></textarea>
			</td>
		</tr>
		
		<tr style="padding-top: 20px">
			<td class="bline" colspan="4" align="center">
				<input type="hidden" name="entity.id" value="<s:property value='entity.id'/>" />
				<input type="hidden" name="entity.state" value="<s:property value='entity.state'/>" />
				<input type="hidden" name="entity.applyDate" value="<s:property value='entity.applyDate'/>" />
				<input type="hidden" id="operate" name="operate" />
				<s:if test="entity.id != null">
					<input type="button" id="checkin" value="" class="btn_submit" />
				</s:if>
				
				<input type="button" id="save" value="" class="btn_save" />
			</td>
		</tr>
	</table>
<s:if test="entity.id != null">
	<br/>
		        <table width="100%" border="0" cellpadding="0" cellspacing="0">
				  <tr>
				    <td height="25" align="left"><font color="#0066CC"><strong>换入合格证：</strong></font></td>
				    <td height="25" align="right">
				    	<%--TODO:给出提示，先保存提交数据，，再跳转--%>
				  	  <a href="${contextPath}/cm/renew/incf/add.do?parent.id=${entity.id}"><img src="${contextPath}/images/btn_add.png" alt="新增" /></a>
		            </td>
				  </tr>
				</table>
<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
	  <tr> 
	    <td width="3%" class="top-td-width-bg">序号</td>
	    <td width="8%" class="top-td-width-bg">4S店</td>
	    <td width="14%" class="top-td-width-bg">车型</td>
	    <td width="14%" class="top-td-width-bg">合格证号</td>
	    <td width="12%" class="top-td-width-bg">车架号</td>
	    <td width="6%" class="top-td-width-bg">供货价</td>
	    <td width="6%" class="top-td-width-bg">订单号</td>
	    <td width="8%" class="top-td-width-bg">贷款品种号</td>
	    <td width="8%" class="top-td-width-bg">贷款起始日</td>
	    <td width="8%" class="top-td-width-bg">贷款到期日</td>
	    <td class="top-td-width-bg">操作</td>
	  </tr>
	 <s:iterator value="entity.inCertificateRenews" status="st" >
		<tr height="26" align="center" bgcolor="#FFFFFF">
			<td align="center"><s:property value="#st.index+1"/></td>
		    <td align="center">(<s:property value="shop.shopMark"/>)<s:property value="shop.shopName"/></td>
		    <td align="center"><s:property value="motorcycleType"/></td>
		    <td align="center"><s:property value="certificationNum"/></td>
		    <td align="center"><s:property value="chassis"/></td>
		    <td align="center"><s:property value="formatAmount(supplyPrice)"/></td>
		    <td align="center"><s:property value="orderNumber"/></td>
		    <td align="center"><s:property value="draft"/></td>
		    <td align="center"><s:date name="draftBeginDate" format="yyyy-MM-dd"/></td>
		    <td align="center"><s:date name="draftEndDate" format="yyyy-MM-dd"/></td>
			<td align="center">
			    <a href="<s:property value='contextPath'/>/cm/renew/incf/edit.do?currentPage=${currentPage}&entity.id=<s:property value="id"/>"><img src="${contextPath}/images/edit.gif" alt="编辑" width="16" height="13" border="0" /></a>
			    <a href="javascript:deleteData(<s:property value="id"/>);"><img src="${contextPath}/images/del.gif" alt="删除" width="16" height="13" border="0" /></a>
		  </td>
		</tr>
	</s:iterator>
	<tr bgcolor="#F9FCEF"> 
	 <td height="36" colspan="17" align="center">
			<%@ include file="/WEB-INF/view/common/pagination.jsp"%>
	 </td>
	</tr>
	</table>
		  
</s:if>
	<!-- 审核记录开始 -->	
	<s:if test="workflow!=null&&workflow.operates!=null">
		<br/>
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
		  <tr>
		    <td height="25" align="left"><font color="#0066CC"><strong>流程操作记录：</strong></font></td>
		    <td height="25" align="right">
		    </td>
		  </tr>
		</table> 
		<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
		  <tr> 
		    <td width="3%" class="top-td-width-bg">序号</td>
		    <td width="20%" class="top-td-width-bg">操作人</td>
		    <td width="20%" class="top-td-width-bg">操作类型</td>
		    <td width="40%" class="top-td-width-bg">描述</td>
		    <td width="17%" class="top-td-width-bg">操作时间</td>
		  </tr>
		 <s:iterator value="workflow.operates" status="stx" >
			<tr height="26" align="center" bgcolor="#FFFFFF" onmousemove="javascript:this.bgColor='#FCFDEE';" onmouseout="javascript:this.bgColor='#FFFFFF';"> 
			 	<td align="center"><s:property value="#stx.index+1"/></td>
			    <td align="center"><s:property value="operater.realName"/></td>
			    <td align="center"><s:property value="type.name"/></td>
			    <td align="center"><s:property value="description"/></td>
			    <td align="center"><s:property value="operateDate"/></td>
			</tr>
		</s:iterator>
		</table>
		<br/>
	</s:if>